/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;


import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.datatables.PetTable;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1PcInventory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.item.L1ItemId;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.templates.L1Npc;
import l1j.server.server.templates.L1Pet;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * 處理收到由客戶端傳來選擇清單的封包
 */
public class C_SelectList extends ClientBasePacket {

	private static final String C_SELECT_LIST = "[C] C_SelectList";
	private static final Logger _log = Logger.getLogger(C_SelectList.class.getName());
	@Override
	public String getType() {
		return C_SELECT_LIST;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}
			// アイテム毎にリクエストが来る。
			final int itemObjectId = this.readD();
			final int npcObjectId = this.readD();


			if (npcObjectId != 0) { // 武器的修理
				final L1Object obj = L1World.getInstance().findObject(npcObjectId);
				if (obj != null) {
					if (obj instanceof L1NpcInstance) {
						final L1NpcInstance npc = (L1NpcInstance) obj;
						final int difflocx = Math.abs(pc.getX() - npc.getX());
						final int difflocy = Math.abs(pc.getY() - npc.getY());
						// 3格以上的距離視為無效請求
						if (difflocx > 3 || difflocy > 3) {
							return;
						}
					}
				}

				final L1PcInventory pcInventory = pc.getInventory();
				final L1ItemInstance item = pcInventory.getItem(itemObjectId);
				final int cost = item.get_durability() * 200;
				if (!pc.getInventory().consumeItem(L1ItemId.ADENA, cost)) {
					return;
				}
				item.set_durability(0);
				pcInventory.updateItem(item, L1PcInventory.COL_DURABILITY);
			} else { // ペットの引き出し
				int petCost = 0;
				int petCount = 0;
				int divisor = 6;
				final Object[] petList = pc.getPetList().values().toArray();
				for (final Object pet : petList) {
					petCost += ((L1NpcInstance) pet).getPetcost();
				}
				int charisma = pc.getCha();
				if (pc.isCrown()) { // 君主
					charisma += 6;
				} else if (pc.isElf()) { // エルフ
					charisma += 12;
				} else if (pc.isWizard()) { // WIZ
					charisma += 6;
				} else if (pc.isDarkelf()) { // DE
					charisma += 6;
				} else if (pc.isDragonKnight()) { // ドラゴンナイト
					charisma += 6;
				} else if (pc.isIllusionist()) { // イリュージョニスト
					charisma += 6;
				}

				final L1Pet l1pet = PetTable.getInstance().getTemplate(itemObjectId);
				if (l1pet != null) {
					final int npcId = l1pet.get_npcid();
					charisma -= petCost;
					if (npcId == 45313 || npcId == 45710 // タイガー、バトルタイガー
							|| npcId == 45711 || npcId == 45712) { // 紀州犬の子犬、紀州犬
						divisor = 12;
					} else {
						divisor = 6;
					}
					petCount = charisma / divisor;
					if (petCount <= 0) {
						pc.sendPackets(new S_ServerMessage(489)); // 引き取ろうとするペットが多すぎます。
						return;
					}
					final L1Npc npcTemp = NpcTable.getInstance().getTemplate(npcId);
					final L1PetInstance pet = new L1PetInstance(npcTemp, pc, l1pet);
					pet.setPetcost(divisor);
				}
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
